<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-07-24
 * Time: 16:04
 */

namespace App\Http\Controllers\Api\Member;

use App\Http\Controllers\Api\BaseAdminController;
use App\Http\Requests\User\DeleteUserRequest;
use App\Http\Requests\User\AddManageUserRequest;
use App\Http\Requests\User\DeleteManageUserRequest;
use App\Http\Requests\User\GetAllUserRequest;
use App\Http\Requests\User\RecoveryUserRequest;
use App\Http\Requests\User\UpdateManageUserRequest;
use App\Http\Requests\User\ManageSingleRequest;
use App\Http\Requests\User\ManageUserPageRequest;
use App\Http\Requests\User\UserPageRequest;
use App\Repositories\UserRepository;
use App\Services\UserService;
use App\Http\Transformers\User\ManageUserPageTransformer;
use App\Http\Transformers\User\CurrentUserTransformer;
use App\Http\Transformers\User\UserPageTransformer;
use App\Http\Transformers\User\UserTransformer;
use App\Http\Transformers\User\GetUserAuthenticationTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/**
 * Class UserController
 * @package App\Http\Controllers\Api\Admin\V1
 */
class UserController extends BaseAdminController
{
    /**
     * @var UserService
     */
    protected $user_service;

    /**
     * @var UserRepository 注入的UserRepository
     */
    private $userRepository;

    /**
     * UserController constructor.
     * @param UserService $userServices
     * @param UserRepository $userRepository
     */
    public function __construct(UserService $userServices,
                                UserRepository $userRepository){
        $this->user_service = $userServices;
        $this->userRepository = $userRepository;
    }

    /**
     * @api {get} /user/current 获取当前登录用户基础信息
     * @apiDescription 获取当前登录用户基础信息
     * @apiGroup user
     * @apiPermission JWT
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     * {
     *      "data": {
     *          "id": 7,
     *          "username": "sss",
     *          "realname": "admin",
     *          "mobile": "15928642015",
     *          "email": "",
     *          "user_type": 2,
     *          "avatar": "http://attachment-ctjypt-test.oss-cn-shanghai.aliyuncs.com/user/avatar/2018328/7b8c2220-2210-4c58-b7ee-488d8c536dcajpg?logo_id=1071",
     *          "gender": 0,
     *          "birthday": null,
     *          "city": "",
     *          "address": "天津市 河东区 东新街道 asdf",
     *          "school_id": 0,
     *          "school_name": "",
     *          "speciality": "",
     *          "signature": null,
     *          "status": 0,
     *          "created_at": "2017-12-05 17:56:37",
     *          "updated_at": "2018-03-28 17:16:52",
     *      }
     * }
     */
    public function current(){
        return $this->response->item(Auth::user(), new CurrentUserTransformer());
    }

    /**
     * 获取用户列表
     */
    public function userPage(UserPageRequest $request){

        //接收参数
        $page = $this->user_service->getUserPage($request->all());

        return $this->response->paginator($page, new UserPageTransformer());
    }

    /**
     * 软删除管理员
     */
    public function deleteManage(DeleteManageUserRequest $request){
        $ids = $request->get('ids');

        if( $this->user_service->deleteManageUser($ids) ){
            return $this->response->created('', '删除成功');
        }
        $this->response->errorInternal('删除失败，请重试！');
    }

    /**
     * 获取管理员列表
     */
    public function manageUserPage(ManageUserPageRequest $request){

        //接收参数
        $page = $this->user_service->getManageUserPage($request->all());

        return $this->response->paginator($page, new ManageUserPageTransformer());
    }

    /**
     * 获取某条管理员数据
     */
    public function managerSingle(ManageSingleRequest $request){
        $id = $request->get('id');

        $data = $this->user_service->getManagerSingle($id);

        return $this->response->item($data, new ManageUserPageTransformer());
    }

    /**
     * 编辑管理员 将角色授权给用户
     */
    public function updateManageUser(UpdateManageUserRequest $request){

        $data = $this->user_service->updateManageUser($request->all());

        if($data['stats']){
            return $this->response->item($data['data'], new ManageUserPageTransformer());
        }
        $this->response->errorInternal($data['msg']);
    }

    /**
     * 新增管理员 将角色授权给用户
     */
    public function addManageUser(AddManageUserRequest $request){

        $data = $this->user_service->addManageUser($request->all());

        if($data['stats']){
            return $this->response->item($data['data'], new ManageUserPageTransformer());
        }
        $this->response->errorInternal($data['msg']);

    }

    /**
     * 获取用户实名认证信息
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function getUserAuthentication(Request $request){
        $userId = $request->id;

        //接收参数
        $authentication = $this->user_service->getUserAuthentication($userId);

        return $this->response->collection($authentication, new GetUserAuthenticationTransformer());
    }

    public function getUserInfo(Request $request){

    }

    /**
     * 删除用户
     * @param DeleteUserRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function deleteUser(DeleteUserRequest $request){
        $ids = $request->get('ids');

        $deletes = $this->userRepository->deleteUserRepository($ids);

        return $this->response->accepted('', 'success');
    }

    /**
     * 恢复被删除用户
     * @param RecoveryUserRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function recoveryUser(RecoveryUserRequest $request){
        $id = $request->get('id');

        $deletes = $this->userRepository->recoveryUserRepository($id);

        return $this->response->accepted('', 'success');
    }

    /**
     * 获取所有用户
     * @param GetAllUserRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function getAllUser(GetAllUserRequest $request){
        $data = $request->all();

        $all = $this->userRepository->getAllUserRepository();

        return $this->response->collection($all, new CurrentUserTransformer());
    }
}
